<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>天气预报查询</title>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        input {
            padding: 8px 12px;
            width: 250px;
            margin-right: 10px;
        }
        button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .result {
            margin-top: 30px;
            padding: 20px;
            border: 1px solid #eee;
            border-radius: 8px;
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>天气预报查询</h1>
        <div>
            <input type="text" id="cityInput">
            <button onclick="queryWeather()">查询天气</button>
        </div>
        <div class="result" id="weatherResult"></div>
    </div>

    <script>
        const API_KEY = "你的聚合数据密钥";
        const API_URL = "http://apis.juhe.cn/simpleWeather/query";

        function queryWeather() {
            let cityInput = document.getElementById("cityInput");
            let city = cityInput.value.trim();
            let resultDiv = document.getElementById("weatherResult");
            let errorDiv = document.getElementById("errorMsg");

            resultDiv.style.display = "none";
            errorDiv.textContent = "";
            if (!city) {
                errorDiv.textContent = "请输入城市名称";
                return;
            }
            let xhr = new XMLHttpRequest();
            let url = `${API_URL}?city=${encodeURIComponent(city)}&key=${API_KEY}`;
            xhr.open('GET', url, true);
            xhr.responseType = 'json';
            xhr.onload = function() {
                if (xhr.status >= 200 && xhr.status < 300) {
                    const data = xhr.response;
                    if (data.reason === "查询成功!") {
                        const weather = data.result.realtime;
                        resultDiv.innerHTML = `
                            <h3>${data.result.city} 实时天气</h3>
                            <p>日期：${weather.date} ${weather.week}</p>
                            <p>天气：${weather.weather}</p>
                            <p>温度：${weather.temperature}°C</p>
                            <p>风向：${weather.direct}</p>
                            <p>风力：${weather.power}</p>
                            <p>湿度：${weather.humidity}%</p>
                        `;
                        resultDiv.style.display = "block";
                    } else {
                        errorDiv.textContent = `查询失败：${data.reason}`;
                    }
                } else {
                    errorDiv.textContent = `请求失败，状态码：${xhr.status}`;
                }
            };
            xhr.send();
        }
    </script>
</body>
</html>